<?php
namespace Home\Controller;
class SearchController extends CommonController{
	public $total_num = 0;
	public $court_data = '';

	public function index(){
		$this->add_step('搜索');
		$keyword   = $this->_get('keyword');
		$agreement = $this->agreement($keyword);
		$article   = $this->article($keyword);
		$court     = $this->court($keyword);
		$this->assign('article', $article);
		$this->assign('agreement', $agreement);
		$this->assign('keyword', $keyword);
		$this->assign('court', $court);
		$this->assign('top_contract_dispute_num', $this->top_contract_dispute_num($keyword));
		$this->assign('top_contract_dispute_money', $this->top_contract_dispute_money($keyword));
		$this->assign('top_law_firm_data', $this->top_law_firm_data($keyword));
		$this->assign('top_to_law_firm_data', $this->top_to_law_firm_data($keyword));
		$this->assign('top_court_name', $this->top_court_name($keyword));
		$this->assign('total_num', $this->total_num);
		$this->display();
	}
	public function agreement($keyword = ''){
		if($keyword){
			$where['title|description|keyword|content'] = array('like', '%' . $keyword . '%');
			$where['is_delete'] = 0;
			$count = D('FreeAgreement')->where($where)->count();
			$list  = D('FreeAgreement')->where($where)->limit(5)->select();
		}
		$count = $count ? $count : 0;
		$this->total_num += $count;
		$data['count'] = $count;
		$data['list']  = $list ? $list : array();
		return $data;
	}
	public function article($keyword = ''){
		if($keyword){
			$where['title|description|related_keyword'] = array('like', '%' . $keyword . '%');
			$where['is_delete'] = 0;
			$count = D('Article')->where($where)->count();
			$list  = D('Article')->where($where)->limit(5)->select();
		}
		$count = $count ? $count : 0;
		$this->total_num += $count;
		$data['count'] = $count;
		$data['list']  = $list ? $list : array();
		return $data;
	}
	public function court($keyword = ''){
		if($keyword){
			$where['title|content_text'] = array('like', '%' . $keyword . '%');
			$count = D('CourtArticle')->where($where)->count();
			$list  = D('CourtArticle')->where($where)->limit(10000)->select();
		}
		$this->court_data = $list ? $list : array();
		$count = $count ? $count : 0;
		$this->total_num += $count;
		$data['count'] = $count;
		$data['list']  = $list ? $list : array();
		return $data;
	}
	// 承办法院
	public function top_court_name($keyword = ''){
		if($keyword){
			$where['title|content_text'] = array('like', '%' . $keyword . '%');
			$list  = D('CourtArticle')->field('count(*) num, court_name name')->where($where)->group('court_name')->order('num desc')->limit(5)->select();
			$count = 0;
			foreach ($list as $rs) {
				$count += $rs['num'];
			}
			foreach ($list as $rs) {
				$rs['rate'] = round($rs['num'] / $count * 100);
				$new_list[] = $rs;
			}
		}
		return $new_list ? $new_list : array();
	}
	// 合同纠纷数量
	public function top_contract_dispute_num($keyword = ''){
		if(!$keyword){
			return false;
		}
		$where['title|content_text'] = array('like', '%' . $keyword . '%');
		$where['judgment_date'] = array('egt', (date('Y') - 3) . '-01-01');
		$list  = D('CourtArticle')->field('DATE_FORMAT(judgment_date,"%Y") year, count(*) num')->where($where)->group('year')->order('year asc')->select();
		
		return $list;
	}
	// 合同纠纷金额
	public function top_contract_dispute_money(){
		$money = 0;
		foreach ($this->court_data as $rs) {
			preg_match("/人民币(.*?)万元/si", $rs['content_text'], $content);
			$money = str_replace(
				array('余'),
				'',
				$content[1]
			);
			$year = date("Y", strtotime($rs['judgment_date']));
			$year_money[$year] += $money;
		}
		unset($rs);
		foreach ($year_money as $year => $money) {
			if($year >= (date('Y') - 3)){
				$rs['year']  = $year;
				$rs['money'] = $money;
				$new_list[] = $rs;
			}
		}
		// 排序
		foreach ($new_list as $key => $row) {
			$order_year[$key] = $row['year'];
		}
		if(!empty($new_list)){
			array_multisort($order_year, SORT_ASC, $new_list);
		}
		
		return $new_list;
	}
	public function top_law_firm_data(){
		
		foreach ($this->court_data as $rs) {
			preg_match("/诉讼代理人(.*?)律师。/si", $rs['content_text'], $content);
			$law_firm = str_replace(
				array('（特别授权代理）', '（实习）'),
				'',
				$content[1]
			);
			if(!empty($content)){
				$row = explode('，', $law_firm);
				$lawyer_list = explode('、', $row[0]);
				$law_firm = $row[1];
				foreach ($lawyer_list as $lawyer) {
					$lawyer_row['realname']   = $lawyer;
					$lawyer_row['law_firm']   = $law_firm;
					$lawyer_data[] = $lawyer_row;
					$lawyer_num[$lawyer]++;
				}
				$law_firm_row['name'] = $law_firm;
				$law_firm_num[$law_firm]++;
			}
		}
		$count = 0;
		unset($rs);
		foreach ($lawyer_num as $name => $num) {
			$count += $num;
		}
		foreach ($lawyer_data as $rs) {
			$rs['num'] = $lawyer_num[$rs['realname']];
			$rs['rate'] = round($rs['num'] / $count * 100);
			$lawyer_data_new[] = $rs;
		}
		$lawyer_data = $lawyer_data_new;
		$count = 0;
		foreach ($law_firm_num as $name => $num) {
			$count += $num;
		}
		unset($rs);
		foreach ($law_firm_num as $name => $num) {
			$rs['name'] = $name;
			$rs['num']  = $num;
			$rs['rate'] = round($rs['num'] / $count * 100);
			$law_firm_data[] = $rs;
		}
		// 排序
		foreach ($lawyer_data as $key => $row) {
			$lawyer_order[$key] = $row['num'];
		}
		if(!empty($lawyer_data)){
			array_multisort($lawyer_order, SORT_DESC, $lawyer_data);
		}
		// 排序
		foreach ($law_firm_data as $key => $row) {
			$law_firm_order[$key] = $row['num'];
		}
		if(!empty($law_firm_data)){
			array_multisort($law_firm_order, SORT_DESC, $law_firm_data);
		}
		$data['lawyer_data']   = $lawyer_data;
		$data['law_firm_data'] = $law_firm_data;
		// var_dump($data);exit;
		
		return $data;
	}
	public function top_to_law_firm_data(){
		
		foreach ($this->court_data as $rs) {
			preg_match("/辩护人(.*?)律师。/si", $rs['content_text'], $content);
			$law_firm = str_replace(
				array('（特别授权代理）', '（实习）'),
				'',
				$content[1]
			);
			if(!empty($content)){
				$row = explode('，', $law_firm);
				$lawyer_list = explode('、', $row[0]);
				$law_firm = $row[1];
				foreach ($lawyer_list as $lawyer) {
					$lawyer_row['realname']   = $lawyer;
					$lawyer_row['law_firm']   = $law_firm;
					$lawyer_data[] = $lawyer_row;
					$lawyer_num[$lawyer]++;
				}
				$law_firm_row['name'] = $law_firm;
				$law_firm_num[$law_firm]++;
			}
		}
		$count = 0;
		unset($rs);
		foreach ($lawyer_num as $name => $num) {
			$count += $num;
		}
		foreach ($lawyer_data as $rs) {
			$rs['num'] = $lawyer_num[$rs['realname']];
			$rs['rate'] = round($rs['num'] / $count * 100);
			$lawyer_data_new[] = $rs;
		}
		$lawyer_data = $lawyer_data_new;
		$count = 0;
		foreach ($law_firm_num as $name => $num) {
			$count += $num;
		}
		unset($rs);
		foreach ($law_firm_num as $name => $num) {
			$rs['name'] = $name;
			$rs['num']  = $num;
			$rs['rate'] = round($rs['num'] / $count * 100);
			$law_firm_data[] = $rs;
		}
		// 排序
		foreach ($lawyer_data as $key => $row) {
			$lawyer_order[$key] = $row['num'];
		}
		if(!empty($lawyer_data)){
			array_multisort($lawyer_order, SORT_DESC, $lawyer_data);
		}
		// 排序
		foreach ($law_firm_data as $key => $row) {
			$law_firm_order[$key] = $row['num'];
		}
		if(!empty($law_firm_data)){
			array_multisort($law_firm_order, SORT_DESC, $law_firm_data);
		}
		$data['lawyer_data']   = $lawyer_data;
		$data['law_firm_data'] = $law_firm_data;
		// var_dump($data);exit;
		
		return $data;
	}


}